<?php

/**
 * Description of ServiceController
 *
 * @author trangnguyen
 * @since 21-11-2013 - 09:18:41
 */
class MyServiceController extends MyController
{

    public $serviceName = '';
    public $typePost = false;
    public $service;
    public $video_show;
    public $name_box;
    public $box_posts;
    public $support_1;
    public $support_2;
    public $albumUrl = "#";
    public $videoUrl = '#';
    public $box_news_url;
    public $box_news_id;
    public $box_boxVideoHomePage_url;
    public $box_boxVideoIndex_url;
    public $box_video_id;
    public $box_album_url;
    public $box_album_id;
    public $borderType = FALSE;
    public $typeViewAll = FALSE;
    public $isMobile = FALSE;

    const MOBILE_THEME = 'services_mobile';
    const DESKTOP_THEME = 'services';

    public function filters()
    {

        parent::filters();

        // lấy theme cho Intro
        $this->detectDevice();

        //Gan bien baseUrl vao bien assetDir
        $this->assetDir = Yii::app()->theme->baseUrl . '/assets/';
        if (!isset($_GET['service']))
            throw new CHttpException(400, 'Invalid request. Please do not repeat this request again.');
        $this->service = $this->getService();
        $this->albumUrl = Yii::app()->createUrl('service/library', array('lang' => Yii::app()->language, 'service' => @$this->service->url));
        $this->videoUrl = Yii::app()->createUrl('service/video', array('lang' => Yii::app()->language, 'service' => @$this->service->url));
        /*
         * Phân trang bằng ajax
         */
        $this->box_news_url = $this->createUrl("service/boxNews", array('lang' => Yii::app()->language, 'service' => $_GET['service']));
        $this->box_boxVideoIndex_url = $this->createUrl("service/boxVideoIndex", array('lang' => Yii::app()->language, 'service' => $_GET['service']));
        $this->box_boxVideoHomePage_url = $this->createUrl("service/boxVideoHomePage", array('lang' => Yii::app()->language, 'service' => $_GET['service']));
        $this->box_album_url = $this->createUrl("service/boxAlbum", array('lang' => Yii::app()->language, 'service' => $_GET['service']));
        $this->box_news_id = @$this->service->symbol . 'BoxNews';
        $this->box_album_id = @$this->service->symbol . 'BoxAlbum';
        $this->box_video_id = @$this->service->symbol . 'BoxVideo';

        /*
         * box video
         */
        $cri = new CDbCriteria ();
        $cri->condition = 'services_id = :se and status!=:s ';
        $cri->params = array(
            ':se' => @$this->service->id,
            ':s' => Services::STATUS_DRAFT,
        );
        $cri->order = 'rank DESC , created_at DESC, id DESC';
        $this->video_show = Video::model()->find($cri);

        /*
         * box support online
         */
        $crit = new CDbCriteria ();
        $crit->condition = 'status!=:s and services_id = :ser and type =:t1 or type=:t2';
        $crit->params = array(
            ':s' => SupportOnline::STATUS_DRAFT,
            ':ser' => @$this->service->id,
            ':t1' => SupportOnline::TYPE_YAHOO,
            ':t2' => SupportOnline::TYPE_SKYPE,
        );
        $this->support_1 = SupportOnline::model()->findAll($crit);

        $crit->condition = 'status!=:s and services_id = :ser and type !=:t1 and type!=:t2';
        $crit->params = array(
            ':s' => SupportOnline::STATUS_DRAFT,
            ':ser' => @$this->service->id,
            ':t1' => SupportOnline::TYPE_YAHOO,
            ':t2' => SupportOnline::TYPE_SKYPE,
        );
        $this->support_2 = SupportOnline::model()->findAll($crit);

        /*
         * box tin tức
         */
        $this->name_box = $this->getCategoryByStatus();
    }

    public function detectDevice()
    {
        if (isset(Yii::app()->request->cookies['cookie_device']))
        {
            $device = Yii::app()->request->cookies['cookie_device']->value;
            if ($device == 'mobile')
            {
                $this->isMobile = TRUE;
                Yii::app()->theme = self::MOBILE_THEME;
            }
            else
            {
                Yii::app()->theme = self::DESKTOP_THEME;
            }
        }
        else
        {
            $detect = Yii::app()->mobileDetect;
            if ($detect->isMobile())
            {
                $this->isMobile = TRUE;
                Yii::app()->theme = self::MOBILE_THEME;
            }
            else
            {
                Yii::app()->theme = self::DESKTOP_THEME;
            }
        }
    }

    public function actionChooseDevice()
    {
        if ($_GET['device'] == "mobile")
        {
            Yii::app()->request->cookies['cookie_device'] = new CHttpCookie('cookie_device', 'mobile');
        }
        else
        {
            Yii::app()->request->cookies['cookie_device'] = new CHttpCookie('cookie_device', 'desktop');
        }

        $this->redirect(Yii::app()->createUrl("service/index", array(
                    'lang' => Yii::app()->language,
                    'service' => $_GET['service']
        )));
    }

    public function actionIndex()
    {
        $this->borderType = true;
        $service = $this->getService();
        $cri = $this->getCriteriaNews(FALSE);

        $count = Post::model()->count($cri);
        $pages = new CPagination($count);
        $pages->route = $this->route;
        $pages->pageSize = 9;
        $pages->applyLimit($cri);
        $box_post = Post::model()->findAll($cri);

        $this->render('//site/index', array(
            'name_box' => $service,
            'box_post' => $box_post,
            'pages' => $pages,
        ));
    }

    public function actionLibrary()
    {
        $service = $this->getService();
        $cret = new CDbCriteria(); //lấy album HIGHLIGHT
        $cret->condition = 'status != :s and language_id=:l and services_id =:ser ';
        $cret->params = array(
            ':ser' => @$service->id,
            ':s' => Services::STATUS_DRAFT,
            ':l' => $this->lang_id,
        );
        $cret->order = 'id DESC';

        $count = PhotoAlbum::model()->count($cret);
        $pages = new CPagination($count);
        $pages->route = $this->route;
        $pages->pageSize = 9;
        $pages->applyLimit($cret);
        $albums = PhotoAlbum:: model()->findAll($cret);

        $this->breadscrumbs = array(Yii::t('main', 'Album'));

        $this->render('//library/index', array(
            'albums' => $albums,
            'pages' => $pages,
        ));
    }

    public function actionViewAlbum()
    {
        if (!isset($_GET['service']))
            throw new CHttpException(400, 'Invalid request. Please do not repeat this request again.');
        if (!isset($_GET['url']))
            throw new CHttpException(400, 'Invalid request. Please do not repeat this request again.');

        $service = $this->getService();

        $ret = new CDbCriteria();
        $ret->condition = 'status != :s and language_id=:l and url=:u and services_id =:ser';
        $ret->params = array(
            ':s' => PhotoAlbum::STATUS_DRAFT,
            ':l' => $this->lang_id,
            ':ser' => $service->id,
            ':u' => $_GET['url'],
        );
        $album_photo = PhotoAlbum::model()->find($ret);

        $rit = new CDbCriteria();
        $rit->condition = ' album_id =:al';
        $rit->params = array(
            ':al' => @$album_photo->id,
        );
        $rit->order = 'id DESC';
        $photos = Photo::model()->findAll($rit);
        /*
         * relation Album
         */
        $ret->condition = 'status != :s and language_id =:l and id !=:i and services_id =:ser';
        $ret->params = array(
            ':s' => PhotoAlbum::STATUS_DRAFT,
            ':l' => $this->lang_id,
            ':i' => @$album_photo->id,
            ':ser' => @$service->id,
        );
        $ret->limit = 10;
        $ret->order = 'rank DESC';
        $Al_relations = PhotoAlbum::model()->findAll($ret);

        $this->breadscrumbs = array(
            "Album" => $this->albumUrl,
            $album_photo->name
        );
        $this->render('//library/view', array(
            'photo_Al' => $album_photo,
            'photos' => $photos,
            'relation_album' => $Al_relations,
        ));
    }

    public function actionVideo()
    {
        $this->typeViewAll = true;
        $service = $this->getService();
        $cret = new CDbCriteria(); //lấy album HIGHLIGHT
        $cret->condition = 'status != :s and language_id=:l and services_id =:ser ';
        $cret->params = array(
            ':ser' => @$service->id,
            ':s' => Services::STATUS_DRAFT,
            ':l' => $this->lang_id,
        );
        $cret->order = 'id DESC , status DESC';

        $count = Video::model()->count($cret);
        $pages = new CPagination($count);
        $pages->route = $this->route;
        $pages->pageSize = 9;
        $pages->applyLimit($cret);

        $videos = Video:: model()->findAll($cret);

        $this->breadscrumbs = array(Yii::t('library', 'Video'));
        $this->render('//video/index', array(
            'videos' => $videos,
            'pages' => $pages,
        ));
    }

    public function actionViewVideo()
    {
        if (!isset($_GET['service']))
            throw new CHttpException(400, 'Invalid request. Please do not repeat this request again.');
        if (!isset($_GET['url']))
            throw new CHttpException(400, 'Invalid request. Please do not repeat this request again.');
        $service = $this->getService();
        $ret = new CDbCriteria();
        $ret->condition = 'status != :s and language_id=:l and url=:u and services_id =:ser';
        $ret->params = array(
            ':s' => Category::STATUS_DRAFT,
            ':l' => $this->lang_id,
            ':ser' => $service->id,
            ':u' => $_GET['url'],
        );
        $video = Video::model()->find($ret);

        $video->count_view = $video->count_view + 1;
        $video->save();

        /*
         * video liên quan
         */
        $cret = new CDbCriteria();
        $cret->condition = 'status != :s and language_id =:l and id !=:i and services_id =:ser';
        $cret->params = array(
            ':s' => Category::STATUS_DRAFT,
            ':l' => $this->lang_id,
            ':i' => @$video->id,
            ':ser' => $service->id,
        );
        $cret->limit = 9;
        $cret->order = 'created_at DESC';

        $count = Video::model()->count($cret);
        $pages = new CPagination($count);
        $pages->route = $this->route;
        $pages->pageSize = 6;
        $pages->applyLimit($cret);

        $relations = Video::model()->findAll($cret);

        $this->breadscrumbs = array(
            "Video" => $this->videoUrl,
            $video->name
        );
        $this->render('//video/view', array(
            'video' => $video,
            'pages' => $pages,
            'relations' => $relations,
        ));
    }

    public function actionPost()
    {
        $this->typePost = TRUE;
        $service = $this->getService();
        $category = $this->getCategoryByUrl();
        $cri = $this->getCriteriaNews(TRUE);

        $count = Post::model()->count($cri);
        $pages = new CPagination($count);
        $pages->route = $this->route;
        $pages->pageSize = 9;
        $pages->applyLimit($cri);

        $posts = Post::model()->findAll($cri);
        $this->breadscrumbs = array($category->name);
        $this->render('//site/index', array(
            'posts' => $posts,
            'pages' => $pages,
            'category' => $category,
            'name_box' => $service,
        ));
    }

    public function criteriaView()
    {
        if (!isset($_GET['url']))
        {
            throw new CHttpException(400, 'Invalid request. Please do not repeat this request again.');
        }

        $ret = new CDbCriteria();
        $ret->condition = 'language_id=:l and url=:u and revision_is_lastest =:re';
        $ret->params = array(
            ':l' => $this->lang_id,
            ':u' => $_GET['url'],
            ':re' => Post::REVISION,
        );
        return $ret;
    }

    public function viewPost()
    {
        $crit = $this->criteriaView();
        $post = Post::model()->find($crit);

        if ($post == null)
        {
            throw new CHttpException(400, 'Invalid request. Please do not repeat this request again.');
        }


        if ($post->status == Category::STATUS_DRAFT)
        {
            throw new CHttpException(400, 'Invalid request. Please do not repeat this request again.');
        }

        $ret = new CDbCriteria();
        $ret->condition = 'status != :s and language_id=:l and id !=:i and revision_is_lastest = :re and category_id=:cate';
        $ret->params = array(
            ':s' => Category::STATUS_DRAFT,
            ':cate' => $post->category_id,
            ':l' => $this->lang_id,
            ':i' => @$post->id,
            ':re' => Post::REVISION,
        );
        $ret->limit = 9;
        $ret->order = 'revision_id DESC';
        $post_relation = Post::model()->findAll($ret);
        $this->breadscrumbs = array(
            $post->category->name => $post->category->getPublicUrl(),
            $post->title
        );
        $this->render('//post/view', array(
            'post' => $post,
            'relations' => $post_relation,
        ));
    }

    public function actionViewPost()
    {
        $crite = $this->criteriaView();
        $page = Page::model()->find($crite);
        if (isset($page))
        {
            $this->viewPage();
        }
        else
        {
            $this->viewPost();
        }
    }

    public function viewPage()
    {
        $crite = $this->criteriaView();
        $page = Page::model()->find($crite);

        if ($page == null)
        {
            throw new CHttpException(400, 'Invalid request. Please do not repeat this request again.');
        }
        if ($page->status == Category::STATUS_DRAFT)
        {
            throw new CHttpException(400, 'Invalid request. Please do not repeat this request again.');
        }

        $this->breadscrumbs = array(
//            $page->category->name => $post->category->getPublicUrl($this->serviceName),
//            $page->title
        );
        $this->render('//post/view', array(
            'page' => $page,
        ));
    }

    public function getService()
    {
        if (!isset($_GET['service']))
            throw new CHttpException(400, 'Invalid request. Please do not repeat this request again.');
        $crit = new CDbCriteria ();
        $crit->condition = 'url =:u and language_id = :l ';
        $crit->params = array(
            ':u' => $_GET['service'],
            ':l' => $this->lang_id,
        );
        return Services::model()->find($crit);
    }

    public function getCriteriaNews($type = FALSE, $limit = '')
    {
        $cri = new CDbCriteria();
        if ($type == false)
        {

            $category = $this->getCategoryByStatus();
        }
        else
        {
            $category = $this->getCategoryByUrl();
        }
        $cri->condition = 'category_id = :ca and status!=:s and revision_is_lastest=:re and language_id = :l';
        $cri->params = array(
            ':s' => Post::STATUS_DRAFT,
            ':re' => Post::REVISION,
            ':ca' => @$category->id,
            ':l' => $this->lang_id,
        );
        $cri->order = 'revision_id DESC, id DESC';
        if ($limit != '')
        {
            $cri->limit = $limit;
        }
        return $cri;
    }

    public function getCategoryByStatus()
    {
        $service_id = $this->getService();
        if ($service_id != null)
        {
            $ret = new CDbCriteria();
            $ret->condition = 'status = :s and services_id = :ser and language_id=:l';
            $ret->params = array(
                ':s' => Category::STATUS_HIGHLIGHT,
                ':ser' => @$service_id->id,
                ':l' => $this->lang_id,
            );
            return Category::model()->find($ret);
        }
    }

    public function getCategoryByUrl()
    {
        if (!isset($_GET['url']))
            throw new CHttpException(400, 'Invalid request. Please do not repeat this request again.');

        $service_id = $this->getService();
        if ($service_id != null)
            $ret = new CDbCriteria();
        $ret->condition = 'status != :s and services_id = :ser and language_id=:l and url=:u';
        $ret->params = array(
            ':s' => Category::STATUS_DRAFT,
            ':ser' => @$service_id->id,
            ':l' => $this->lang_id,
            ':u' => $_GET['url'],
        );
        return Category::model()->find($ret);
    }

    public function actionBoxNews()
    {
        $this->layout = 'blank';
        $name_box = $this->getCategoryByStatus();
        $cri = $this->getCriteriaNews(FALSE, Option::getItem('limit-box-news'));

        $count = Post::model()->count($cri);
        $pages = new CPagination($count);
        $pages->route = $this->route;
        $pages->pageSize = 4;
        $pages->applyLimit($cri);

        $box_posts = Post::model()->findAll($cri);
        $this->render('//post/boxNews', array(
            'name_box' => $name_box,
            'box_posts' => $box_posts,
            'pages' => $pages,
        ));
    }

    public function boxVideo($limit)
    {
        $this->layout = 'blank';
        $cri = new CDbCriteria ();
        $cri->condition = 'services_id = :se and status!=:s ';
        $cri->params = array(
            ':se' => @$this->service->id,
            ':s' => Services::STATUS_DRAFT,
        );
        $cri->order = 'rank DESC , created_at DESC';

        $cri->limit = 15;

        $count = Video::model()->count($cri);
        $pages = new CPagination($count);
        $pages->route = $this->route;
        $pages->pageSize = $limit;
        $pages->applyLimit($cri);

        $videos = Video::model()->findAll($cri);

        $this->render('//video/boxVideo', array(
            'videos' => $videos,
            'pages' => $pages,
        ));
    }

    public function actionBoxAlbum()
    {
        $this->layout = 'blank';
        /*
         * box album ảnh
         */
        $cret = new CDbCriteria();
        $cret->condition = 'status != :s and language_id=:l and services_id =:ser ';
        $cret->params = array(
            ':ser' => @$this->service->id,
            ':s' => Services::STATUS_DRAFT,
            ':l' => $this->lang_id,
        );
        $cret->limit = 5;
        $cret->order = 'rank DESC , id DESC';
        $count = PhotoAlbum::model()->count($cret);
        $pages = new CPagination($count);
        $pages->route = $this->route;
        $pages->pageSize = 1;
        $pages->applyLimit($cret);

        $albums = PhotoAlbum:: model()->findAll($cret);

        $this->render('//library/boxAlbum', array(
            'albums' => $albums,
            'pages' => $pages,
        ));
    }

    public function actionboxVideoIndex()
    {
        $this->boxVideo(10);
    }

    public function actionboxVideoHomePage()
    {
        $this->typeViewAll = true;
        $this->boxVideo(Option::getItem('limit-box-video'));
    }

}

/** 
* End file ServiceController
*/
